<template>
  <div>
     <!-- 预览管理员 渲染数据  -->
     <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>管理员列表</span>
        <el-button type="primary" size="mini" @click="addCateShowDialogBtnFn">添加管理员</el-button>
      </div>
      <el-table
      :data="UserList"
      style="width: 100%">
      <el-table-column
      label="序号"
      type="index"
      width="180">
    </el-table-column>
      <el-table-column
          label="昵称"
          width="180">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.nickname }}</span>
          </template>
    </el-table-column>
      <el-table-column
        label="账号"
        width="300">
        <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.username }}</el-tag>
            </div>
        </template>
      </el-table-column>
      <el-table-column
      label="密码"
      width="300">
      <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.password }}</span>
      </template>
    </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="updateCateBtnFn(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="delCateFn(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    </el-card>
     <!-- 添加管理员 -->
     <el-dialog
      title="添加用户"
      :visible.sync="dialogVisible"
      width="30%"
      @close="dialogCloseFn"
      >
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
        <el-form-item label="用户账号" prop="cate_username">
          <el-input v-model="addForm.cate_username" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="cate_password">
          <el-input v-model="addForm.cate_password" minlength="1" maxlength="15"></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="cate_nickname">
          <el-input v-model="addForm.cate_nickname" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="confirmFn">确 定</el-button>
  </span>
    </el-dialog>
  </div>

</template>

<script>
import { getUserAPI, delUserAPI, addUserAPI, updateUserAPI } from '@/api/user'
export default {
  name: 'HotelSystemAdminUserM',

  data () {
    return {
      adminUserList: [], // 存储所有的管理员信息
      cateList: [], // 文章分类列表
      dialogVisible: false, // 控制对话框的出现/隐藏
      addForm: { // 添加表单的数据对象
        cate_username: '',
        cate_password: '',
        cate_nickname: ''
      },
      addRules: { // 添加表单的验证规则对象
        cate_username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '账号必须是1-10位的非空字符', trigger: 'blur', min: '1' }
        ],
        cate_password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '密码必须是1-15位的字母数字', trigger: 'blur', min: '1' }
        ],
        cate_nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { message: '昵称至少为1个字符', trigger: 'blur', min: '1' }
        ]
      },
      isEdit: false, // true为编辑状态 false为新增状态
      editId: '' // 保存正在要编辑的数据id值
    }
  },
  created () {
    this.getUser()
  },

  methods: {
  // 首页进入加载所有用户列表
    async getUser () {
      const res = await getUserAPI()
      this.UserList = res.data.data
      // console.log(this.UserList)
    },

    async getArtCateFn () {
      const { data: res } = await (this.addForm)
      // console.log(res)
      this.cateList = res.data
    },
    // 点击确定，调用接口  也是增加数据的关键方法
    confirmFn () {
      // 调用前的兜底校验
      this.$refs.addRef.validate(async valid => {
        if (valid) {
          // 通过校验
          //
          if (this.isEdit) {
            // 修改数据
            const id = this.editId
            const { data: res } = await updateUserAPI(this.addForm, id)
            console.log(res)
            // if (res.code !== 0) return this.$message.error(res.message)
            // 更新成功
            // this.$message.success(res.message)
          } else {
            // 新增数据
            const username = this.addForm.cate_username
            const password = this.addForm.cate_password
            const nickname = this.addForm.cate_nickname
            const { data: res } = await addUserAPI({ username, password, nickname })
            console.log(res)
            if (res.code !== 200) return this.$message.error(res.msg)
            // 添加成功
            this.$message.success(res.msg)
          }
          // 重新获取文章分类列表
          this.getUser()
          this.dialogVisible = false
        } else {
          return false
        }
      })
    },
    // 添加分类按钮点击事件，让对话框出现
    addCateShowDialogBtnFn () {
      this.dialogVisible = true
      this.isEdit = false // 变回新增状态
      this.editId = ''
    },
    // 弹窗关闭的回调
    dialogCloseFn () {
      this.$refs.addRef.resetFields()
    },
    // 修改分类按钮的点击事件 先做数据回显
    updateCateBtnFn (obj) {
      // obj的值就是id, cate_name, cate_alias
      console.log(obj)
      this.dialogVisible = true
      this.isEdit = true
      this.editId = obj.id
      this.$nextTick(() => {
        // 数据回显
        this.addForm.cate_nickname = obj.nickname
        this.addForm.cate_password = obj.password
        this.addForm.cate_username = obj.username
      })
    },
    // 删除按钮的点击事件
    async delCateFn (obj) {
      // console.log(obj)
      this.$confirm('将删除此用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const userID = obj.id
        const { data: res } = await delUserAPI(userID)
        console.log(res)
        this.$message({
          type: 'success',
          message: res.msg
        })
        // 重新获取
        this.getUser()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      // 当前点击删除的用户的id

      // const { data: res } = await (obj.id)
      // if (res.code !== 0) return this.$message.error(res.message)
      // // 删除成功
      // this.$message.success(res.message)
      // 重新获取
      // this.getArtCateFn()
    }
  }
}

</script>
<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
